package tech.yqyl;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import tech.yqyl.config.ApplicationConfig;

import java.io.InputStream;

/**
 * @className: YamlUtils
 * @description:
 * @author: caoyangjie
 * @date: 2024/9/6
 **/
public class YamlUtils {
    private static ApplicationConfig applicationConfig;
    public static ApplicationConfig getConfig(){
        if( applicationConfig!=null ) {
            return applicationConfig;
        }
        // 创建 Yaml 实例
        Yaml yaml = new Yaml(new Constructor(ApplicationConfig.class));

        // 读取 YAML 文件
        try (InputStream inputStream = YamlUtils.class
                .getClassLoader()
                .getResourceAsStream("application.yml")) {
            if (inputStream != null) {
                ApplicationConfig config = yaml.load(inputStream);
                applicationConfig = config;
                return config;
            } else {
                System.out.println("Unable to find config.yml");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
